package shu.java.csky.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import shu.java.csky.utils.QiniuUtils;
import shu.java.csky.vo.ResultVO;

import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private QiniuUtils qiniuUtils;

    @PostMapping
    public ResultVO upload(@RequestParam("image")MultipartFile file) {
        if(file == null) {
            return new ResultVO(400, "文件为空", null);
        }
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
        // 上传到七牛云
        boolean upload = qiniuUtils.upload(file, fileName);
        if (upload){
            return new ResultVO(200, "成功", QiniuUtils.url + fileName);
        }
        return new ResultVO(400, "失败", null);
    }
}
